<template>
  <div>
    <FormPanel ref="from" :list="formList" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <FilterTable ref="table" :columns="columns1" :dataSource="tableLists" :isSelectColumn="false" height="auto" :onColumnsChange="columns => (this.columns1 = columns)"> </FilterTable>
    <div style="height:47px;"></div>

    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 2)">驳 回</multiuse-button>
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 3)">同 意</multiuse-button>
    </div>
  </div>
</template>
<script>
import { getDetail, checkStatus, gettmFiAccountType, getLogList } from '@/api/omSale/PurchaseReturn/ReturnAudit/index.js';
import { dictionary } from '@/utils/dictMixin';

export default {
  data() {
    return {
      formList: this.createdFrom(),
      columns1: this.createdTable(),
      fetchParams: {},
      fetchApi: () => {},
      issearch: false,
      tableLists: [],
      json: {},
      dealerCode: ''
    };
  },
  props: ['rowData'],
  created() {
    this.getLogList(this.rowData.id);
    this.getDetail(this.rowData);
  },
  components: {},
  mixins: [dictionary],
  methods: {
    createdFrom() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '退货申请'
        },
        {
          type: 'INPUT',
          label: '退货单号',
          fieldName: 'returnNo',
          placeholder: '请选择',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '经销商',
          fieldName: 'dealerName',
          placeholder: '请输入',
          disabled: true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(1)}></el-button>
              </div>
            );
          }
        },
        {
          type: 'DATE',
          label: '申请日期',
          fieldName: 'applyDate',
          placeholder: '请输入',
          dateType: 'exactdate',
          disabled: true
        },
        {
          type: 'SELECT',
          label: '退车原因',
          fieldName: 'returnReason',
          placeholder: '请输入',
          itemList: this.createDictList('6056'),
          disabled: true
        },
        {
          type: 'INPUT',
          label: '退货人',
          fieldName: 'applyPersonName',
          placeholder: '请输入',
          disabled: true
        },

        {
          type: 'INPUT',
          label: '店内联系人',
          fieldName: 'linkMan',
          placeholder: '请输入',
          disabled: true
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          disabled: true
        },

        {
          type: 'INPUT',
          label: '销售订单号',
          fieldName: 'doNo',
          placeholder: '请输入',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '联系方式',
          fieldName: 'linkPhone',
          placeholder: '请输入',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '产品代码',
          fieldName: 'productName',
          placeholder: '请输入',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '原价格',
          fieldName: 'afterDiscountRebateAmount',
          placeholder: '请输入',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '原发票号',
          fieldName: 'goldenInvoiceNo',
          placeholder: '请输入',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '原税率',
          fieldName: 'rate',
          placeholder: '请输入',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '退车金额',
          fieldName: 'returnAmount',
          placeholder: '请输入',
          disabled: true,
          rules: [{ required: true, message: '请输入', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '店端车辆状态',
          fieldName: 'vehicleStatus',
          placeholder: '请输入',
          disabled: true,
          itemList: this.createDictList('6040')
        },
        {
          type: 'INPUT',
          label: '原订单资金类型',
          fieldName: 'accountTypeName',
          placeholder: '请输入',
          disabled: true
        },
        {
          type: 'SELECT',
          label: '退款账户',
          fieldName: 'returnAccount',
          placeholder: '请选择',
          disabled: true,
          itemList: []
        },

        {
          type: 'TEXT_AREA',
          label: '退车原因说明',
          fieldName: 'returnReasonRemark',
          placeholder: '请输入',
          selfCols: 3,
          disabled: true
        },
        {
          type: 'UPLOAD_FILE',
          label: '附件上传',
          fieldName: 'pos',
          placeholder: '请输入',
          initialValue: [],
          offsetRightCols: 2,
          disabled: true,
          upload: {
            actionUrl: '/api/file/oss/upload',
            limit: 1000,
            tipText: '最多上传1000个附件'
          }
        },
        {
          type: 'TEXT_AREA',
          label: '审核意见',
          fieldName: 'returnRemark',
          placeholder: '请输入',
          selfCols: 3
        },
        {
          type: 'BREAK_SPACE',
          label: '操作记录'
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作状态',
          dataIndex: 'returnStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6055'),
          render: p => {
            return <span>{this.createDictText(p.row.returnStatus, '6055')}</span>;
          }
        },
        {
          title: '操作人员',
          dataIndex: 'createdBy',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          editType: 'select',
          editItems: []
        },
        {
          title: '操作时间',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '审核备注',
          dataIndex: 'remark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    seacherFun() {
      this.issearch = true;
    },
    changeHandle(val) {
      this.saveData = val;
    },
    async gettmFiAccountType() {
      let res = (await gettmFiAccountType({ doNo: this.formList.find(x=>x.fieldName=='doNo').initialValue })).data.map(x => ({ text: x.accountTypeName, value: x.id }));
      this.formList.find(x => x.fieldName == 'returnAccount').itemList = res;
    },
    async getDetail(data) {
      let res = await getDetail({ id: data.id });
      this.dealerCode = res.data.dealerCode;
      if (res.data.pos && res.data.pos.length > 0) {
        res.data.pos.map(x => {
          x.name = x.fileName;
          x.url = x.fileAddress;
        });
      }

      for (let i in res.data) {
        this.formList.map(x => {
          if (x.fieldName == i) {
            x.initialValue = res.data[i];
          }
        });
      }
      this.json = res.data;
      this.gettmFiAccountType();
    },
    async closeDrawer(type) {
      if (type == 1) {
        this.$emit('close');
      } else if (type == 3) {
        let str = this.formList.find(x => x.fieldName == 'returnRemark').initialValue;
        let json = this.json;
        json.returnRemark = str;
        json.returnStatus = 60551003;
        json.id = this.rowData.id;
        let res = await checkStatus(json);
        if (res.resultCode == 200) {
          this.$notify({
            title: '成功',
            message: '同意成功',
            type: 'success'
          });
          this.$emit('close', json);
        }
      } else {
        let json = this.json;
        json.id = this.rowData.id;
        let str = this.formList.find(x => x.fieldName == 'returnRemark').initialValue;
        if (!str) {
          this.$notify({
            title: '警告',
            message: '请填写审核意见',
            type: 'warning'
          });
          return;
        }
        json.returnRemark = str;
        json.returnStatus = 60551004;
        let res = await checkStatus(json);
        if (res.resultCode == 200) {
          this.$notify({
            title: '成功',
            message: '驳回成功',
            type: 'success'
          });
          this.$emit('close', json);
        }
      }
    },
    async getLogList(id) {
      const res1 = await getLogList({ id: id });
      if (res1.resultCode == 200) {
        this.tableLists = res1.data;
      }
    }
  }
};
</script>
